/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BalanceteAnual.java
 *
 * Created on 17/05/2009, 21:52:33
 */

package gerenciadorfinanceiro.view.resources;

import gerenciadorfinanceiro.Utilitario;
import gerenciadorfinanceiro.database.resources.Income;
import gerenciadorfinanceiro.database.resources.Outcome;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.application.Action;

/**
 *
 * @author eduardo
 */
public class BalanceteAnual extends javax.swing.JDialog {

    /** Creates new form BalanceteAnual */
    public BalanceteAnual() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblBalancete = new javax.swing.JTable();
        cbxAno = new javax.swing.JComboBox();
        btnGerar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gerenciadorfinanceiro.GerenciadorFinanceiroApp.class).getContext().getResourceMap(BalanceteAnual.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setModal(true);
        setName("Form"); // NOI18N
        setResizable(false);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tblBalancete.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Mes", "Receita", "Despesa", "Saldo"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tblBalancete.setName("tblBalancete"); // NOI18N
        jScrollPane1.setViewportView(tblBalancete);

        cbxAno.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        cbxAno.setName("cbxAno"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(gerenciadorfinanceiro.GerenciadorFinanceiroApp.class).getContext().getActionMap(BalanceteAnual.class, this);
        btnGerar.setAction(actionMap.get("Gerar")); // NOI18N
        btnGerar.setText(resourceMap.getString("btnGerar.text")); // NOI18N
        btnGerar.setName("btnGerar"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(cbxAno, javax.swing.GroupLayout.PREFERRED_SIZE, 446, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnGerar, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 566, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbxAno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnGerar))
                .addGap(12, 12, 12)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private String[] columns = {"Mes","Receita","Despesa","Saldo"};
    private String[] meses = {"Janeiro","Fevereiro","Marco","Abril","Maio","Junho","Julho",
        "Agosto","Setembro", "Outubro", "Novembro", "Dezembro"};

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        Calendar c = new GregorianCalendar();
        int atual = c.get(Calendar.YEAR);

        c.add(Calendar.YEAR, -5);

        cbxAno.removeAllItems();

        int year = c.get(Calendar.YEAR);

        for (int i = 0; i < 10; i++) {
            cbxAno.addItem(year+i);
        }

        cbxAno.setSelectedItem(atual);

        DefaultTableModel tm = new DefaultTableModel();
        tm.setColumnIdentifiers(columns);

        tblBalancete.setModel(tm);
    }//GEN-LAST:event_formComponentShown

    @Action
    public void Gerar() {
        DecimalFormat df  = new DecimalFormat();
        df.applyPattern("R$ ###,##0.00;R$ -###,##0.00");
        DefaultTableModel tm = new DefaultTableModel();
        tm.setColumnIdentifiers(columns);

        double receitaTotalAnual = 0;
        double despesaTotalAnual = 0;

        for (int i = 0; i < meses.length; i++) {
            List<Income> incomes = this.util.getIncomeDAO().getIncomes(this.util.getLoggedUser(), i + 1, ((Integer)cbxAno.getSelectedItem()).intValue());
            List<Outcome> outocomes = this.util.getOutcomeDAO().getAllOucomes(this.util.getLoggedUser(), i + 1, ((Integer)cbxAno.getSelectedItem()).intValue());

            double receitaTotal = 0;
            double despesaTotal = 0;

            for(Outcome out: outocomes) {
                despesaTotal += out.getValue();
            }

            for(Income inc: incomes) {
                receitaTotal += inc.getValue();
            }

            receitaTotalAnual += Math.ceil(receitaTotal);
            despesaTotalAnual += Math.floor(despesaTotal);

            tm.addRow(new String[] {meses[i], df.format(Math.ceil(receitaTotal))+"", df.format(Math.floor(despesaTotal))+"", df.format((Math.ceil(receitaTotal)-Math.floor(despesaTotal)))+""});
        }

        tm.addRow(new String[] {"Total", df.format(receitaTotalAnual)+"", df.format(despesaTotalAnual)+"", df.format((receitaTotalAnual-despesaTotalAnual))+""});
        tblBalancete.setModel(tm);

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGerar;
    private javax.swing.JComboBox cbxAno;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblBalancete;
    // End of variables declaration//GEN-END:variables

    private Utilitario util;

    /**
     * @return the util
     */
    public Utilitario getUtil() {
        return util;
    }

    /**
     * @param util the util to set
     */
    public void setUtil(Utilitario util) {
        this.util = util;
    }
}
